#!/usr/bin/perl 
#             -w  affiche warning
# 0 : date lim | 1 : extention fichier | 2 : path des data | 3 : format dossier 
##########################################################################################################################
##########################################################################################################################

my $out = '' ;
my $item = '' ; 
my $irec = 0 ;
my $ista = 0 ;
my $icomp = 0 ;
my %lesrec ; 
my %lesstat ; 
my %lescomp ; 


 
open (OUT,">tmp.txt");
open (OUT1,">tmp1.txt");
open (OUT2,">tmp2.txt");
open (OUT3,">tmp3.txt");
open (OUT4,">tmp4.txt"); 
open (OUT5,">tmp5.txt"); 
open (OUT6,">tmp6.txt");

my @list = `ls -dl $ARGV[2]/*/$ARGV[3] $ARGV[2]/$ARGV[3] $ARGV[2]/*/*/$ARGV[3] | grep drwxr` ;
my $numb =  $#list + 1; 

foreach $item (@list) {
    chomp($item) ;
    my @elt = split('/',$item) ;  
    my @elt = split(' ',$item) ; 
    my $paf = $elt[-1] ;

    my @sublist = `ls $paf/$ARGV[1].sac` ;

    foreach $file (@sublist) {
        chomp($file);
        
        my @elt=split("/",$file);

        my $direct = substr($elt[-1],11,14) ; 
        my $stat = substr($elt[-1],7,3) ; 
        my $compo = substr($elt[-1],29,1) ;
        
        if(-e $file) { 
            print OUT "$file\n" ;

            if(exists $lesrec{$direct}) {} else {$lesrec{$direct} = ++$irec ; print OUT4 "$paf/$direct\n" ; }
            if(exists $lesstat{$stat})  {} else {$lesstat{$stat}  = ++$ista ; print OUT5 "$stat\n" ;}
            if(exists $lescomp{$compo}) {} else {$lescomp{$compo} = ++$icomp; print OUT6 "$compo\n" ; }

            print OUT1 "$lesrec{$direct}\n" ; 
            print OUT2 "$lesstat{$stat}\n" ;
            print OUT3 "$lescomp{$compo}\n" ;

        }
    }
}
close(OUT) ; 
close(OUT1) ; 
close(OUT2) ; 
close(OUT3) ;
close(OUT4) ;
close(OUT5) ;
close(OUT6) ;

#open (OUT4,">tmp4.txt"); foreach my $k (keys(%lesrec)) { print OUT4 "$k\n" ;} close(OUT4) ;
#open (OUT5,">tmp5.txt"); foreach my $k (keys(%lesstat)) { print OUT5 "$k\n" ;} close(OUT5) ;
#open (OUT6,">tmp6.txt"); foreach my $k (keys(%lescomp)) { print OUT6 "$k\n" ;} close(OUT6) ;

